package com.murfrosoft.entity.AirHockey;


import java.util.HashMap;

import com.murfrosoft.R;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;



public class Puck {

	private float radius;
	private float positionX;
	private float positionY;
	private float velocityX;
	private float velocityY;
	private float friction;
	private float collisionX;
	private float collisionY;
	private Bitmap bitmap;
	private SoundPool soundPool;

	private HashMap<Integer,Integer> soundMap;
	
	public Puck(Context context){
		soundPool=new SoundPool(20,AudioManager.STREAM_MUSIC,0);
		soundMap=new HashMap<Integer,Integer>();
		soundMap.put(0, soundPool.load(context,R.raw.click,1));
		soundMap.put(1, soundPool.load(context,R.raw.boing,2));
       
		
	}
	
	public void setBitmap(Bitmap bitmap){
		this.bitmap=bitmap;
	}
	
	public float getRadius() {
		return radius;
	}
	public void setRadius(float radius) {
		this.radius = radius;
	}
	public float getPositionX() {
		return positionX;
	}
	public void setPositionX(float positionX) {
		this.positionX = positionX;
	}
	public float getPositionY() {
		return positionY;
	}
	public void setPositionY(float positionY) {
		this.positionY = positionY;
	}
	public float getVelocityX() {
		return velocityX;
	}
	public void setVelocityX(float velocityX) {
		this.velocityX = velocityX;
	}
	public float getVelocityY() {
		return velocityY;
	}
	public void setVelocityY(float velocityY) {
		this.velocityY = velocityY;
	}
	public float getFriction() {
		return friction;
	}
	public void setFriction(float friction) {
		this.friction = friction;
	}
	
	public float getCollisionX() {
		return collisionX;
	}
	
	public float getCollisionY() {
		return collisionY;
	}
	
	public void move(int width,int height){
		positionX = positionX + velocityX;
		positionY = positionY + velocityY;
		
		/* Bounce off side walls */
		if(positionX + velocityX <= 0 + radius || positionX + velocityX >= width - radius){
			soundPool.play(soundMap.get(1), 1, 1, 0, 1, 1f);
			velocityX = velocityX * -1;
			
		}
		
		/* Bounce off back walls IF Not in goal area --- NOT EXACT NUMBERS HERE */
		if( positionX < 0.3f*height/2 || positionX > 0.7f*height/2 ){
			if(positionY + velocityY <= 0 + radius || positionY + velocityY >= height - radius){
				soundPool.play(soundMap.get(1), 1, 1, 0, 1, 1f);
				velocityY = velocityY * -1;
			}
		}
		
		
		/* Add Puck Friction */
		velocityX *= friction;
		velocityY *= friction;
		
		/* Force puck in-bounds */
		if( positionX < radius ){
			positionX = radius;
		}
		else if( positionX > width - radius ){
			positionX = width - radius;
		}
		/* Only keep in-bounds if NOT in goal area --- NOT EXACT NUMBERS HERE! */
		if( positionX < 0.3f*height/2 || positionX > 0.7f*height/2 ){
			if( positionY < radius ){
				positionY = radius;
			}
			else if( positionY > height - radius ){
				positionY = height - radius;
			}
		}
		
	}
	
	public void reset(int width, int height, int side){
		if( side == 1 ){		// if computer just scored, move puck toward you on reset
			velocityX = 1;
			velocityY = 1;
		}
		else if( side == 2 ){   // if you scored, move puck toward opponent
			velocityX = -1;
			velocityY = -1;
		}
		else{
			velocityX = 0;
			velocityY = 0;
		}
		
		positionX = width/2;
		positionY = height/2;
	}
	
	public void do_collision(Paddle p){
		/* First, calculate distance between puck center and Paddle center */
		float MAX_VELOCITY = 25;
		float r_sum = radius + p.radius;
		float a = (positionX - p.positionX) * (positionX - p.positionX);
		float b = (positionY - p.positionY) * (positionY - p.positionY);
		float f = 0.90f;
				
		if( Math.sqrt(a + b) <= r_sum ){
			soundPool.play(soundMap.get(0),1f,1f,1,0,1f);
			/* If both puck and paddle velocities are + or =, then add the two velocities */
			if( (velocityX <= 0 && p.velocityX <= 0) || (velocityX > 0 && p.velocityX > 0) ){
				velocityX += p.velocityX * f;
			}
			/* else, flip puck velocity and add paddle */
			else{
				velocityX = ((-1)*velocityX + p.velocityX) * f;
			}
			
			if( (velocityY <= 0 && p.velocityY <= 0) || (velocityY > 0 && p.velocityY > 0) ){
				velocityY += p.velocityY * f;
			}
			/* else, flip puck velocity and add paddle */
			else{
				velocityY = ((-1)*velocityY + p.velocityY) * f;
			}
			
			if( velocityX > MAX_VELOCITY ){
				velocityX = MAX_VELOCITY;
			}
			if( velocityY > MAX_VELOCITY ){
				velocityY = MAX_VELOCITY;
			}
		}	
	}
	
	/*********************************************************************
	 * Trying some new collision math here...  may screw some things up!
	 * @param p
	 *********************************************************************/
	public void do_collision2(Paddle p){
		/* First, calculate distance between puck center and Paddle center */
		float MAX_VELOCITY = 15;
		float r_sum = radius + p.radius;
		float a = (positionX - p.positionX) * (positionX - p.positionX);
		float b = (positionY - p.positionY) * (positionY - p.positionY);
		float f = 0.90f;
				
		if( Math.sqrt(a + b) <= r_sum ){
			soundPool.play(soundMap.get(0),1f,1f,1,0,1f);
			collisionX = (positionX + p.positionX)/2;
			collisionY = (positionY + p.positionY)/2;
			//float cos_theta = (p.positionX - positionX)/(2 * radius);
			//float sin_theta = (p.positionY - positionY)/(2 * radius);
			velocityX += 2 * p.velocityX;
			velocityY += 2 * p.velocityY;
			//velocityX += p.velocityX * cos_theta * f;
			//velocityY += p.velocityY * sin_theta * f;
			
			/* Check for the four quadrants of collision */
			// Puck is upper left
			if( positionX < collisionX && positionY < collisionY ){
				velocityX = (-1) * Math.abs(velocityX) + 2 * p.velocityX;
				velocityY = (-1) * Math.abs(velocityY) + 2 * p.velocityY;
			}
			// Puck is upper right
			else if( positionX >= collisionX && positionY < collisionY ){
				velocityX = Math.abs(velocityX) + 2 * p.velocityX;
				velocityY = (-1) * Math.abs(velocityY) + 2 * p.velocityY;
			}
			// Puck is lower left
			else if( positionX < collisionX && positionY >= collisionY ){
				velocityX = (-1) * Math.abs(velocityX) + 2 * p.velocityX;
				velocityY = Math.abs(velocityY) + 2 * p.velocityY;
			}
			// Puck is lower right
			else{
				velocityX = Math.abs(velocityX) + 2 * p.velocityX;
				velocityY = Math.abs(velocityY) + 2 * p.velocityY;
			}
			
			/* If both puck and paddle velocities are + or =, then add the two velocities *
			if( (velocityX <= 0 && p.velocityX <= 0) || (velocityX > 0 && p.velocityX > 0) ){
				velocityX += p.velocityX * cos_theta * f;
			}
			/* else, flip puck velocity and add paddle *
			else{
				velocityX = (-1) * velocityX + p.velocityX * cos_theta * f;
			}
			
			if( (velocityY <= 0 && p.velocityY <= 0) || (velocityY > 0 && p.velocityY > 0) ){
				velocityY += p.velocityY * sin_theta * f;
			}
			/* else, flip puck velocity and add paddle *
			else{
				velocityY = (-1) * velocityY + p.velocityY * sin_theta * f;
			}
			*/
		}
		else{
			collisionX = 0;
			collisionY = 0;
		}
		
		if( velocityX > MAX_VELOCITY ){
			velocityX = MAX_VELOCITY;
		}
		if( velocityY > MAX_VELOCITY ){
			velocityY = MAX_VELOCITY;
		}
	}
	
	/* check score will return a -1 if player scores, +1 if computer scores, and 0 of no scores.
	 * THIS IS JUST A TEST!!!
	 */
	public int check_score(int width, int height){
		
		int s = 0;
		if(positionY <= radius){
			s = -1;
		}
		else if( positionY >= height-radius){
			s = 1;
		}
		else{
			s = 0;
		}
		return s;
		
	}
	
	public void draw(Canvas canvas){
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		//canvas.drawCircle(positionX, positionY, radius, paint);
		canvas.drawBitmap(bitmap,positionX-radius,positionY-radius,paint);
		
		/* If collision has occurred, draw a small yellow circle where collision happens */
		if( collisionX > 0 && collisionY > 0){
			paint.setColor(Color.YELLOW);
			canvas.drawCircle(collisionX, collisionY, 30, paint);
			paint.setColor(Color.BLACK);
			paint.setStrokeWidth(5);
			float dX = positionX - collisionX;
			float dY = positionY - collisionY;
			canvas.drawLine(collisionX, collisionY, collisionX + 3*dX, collisionY + 3*dY, paint);
			canvas.drawLine(collisionX - 3*dY, collisionY + 3*dX, collisionX + 3*dY, collisionY - 3*dX, paint);
		}

	}


}
